

//
//  MyTapBiClassGetMoneyViewController.m
//  VEMoneyPorject
//
//  Created by 陈建 on 2018/9/28.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "MyTapBiClassGetMoneyViewController.h"
#import "MyTapBiClassModel.h"
#import "MyTapBiHeaderView.h"
#import "MyTapBiResuleTableViewCell.h"
#import "VESaveMoneyTableViewCell.h"
#import "DTLongButtonView.h"
@interface MyTapBiClassGetMoneyViewController ()

@property (nonatomic, assign)double enterMoney;

@property (nonatomic, strong)BRTextField *guige;

@property (nonatomic, strong)NSString *guigeStr;

@end

@implementation MyTapBiClassGetMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.refreshType = CJBaseTableVcRefreshTypeNone;
    MyTapBiHeaderView *headerView = [[MyTapBiHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    self.tableView.tableHeaderView = headerView;
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)loadData{
    [super loadData];
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:[NSString stringWithFormat:@"%@/VEService/GetLILV.do",kPublicInterfaceAPI] isPost:YES withParameters:@{@"Page":@"1"}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        self.isSuccessNet = YES;
        NSLog(@"%@",response);
        NSArray *arr = response;
        self.dataArray = [MyTapBiClassModel mj_objectArrayWithKeyValuesArray:arr];
        
        for (NSInteger i = 0; i<self.dataArray.count; i++) {
            MyTapBiClassModel *model = self.dataArray[i];
            NSArray *firsArr = [model.ZONGSHUJU componentsSeparatedByString:@","];
            model.hunheArray = firsArr;
            NSMutableArray *ssA = [NSMutableArray array];
            NSMutableArray *rrA = [NSMutableArray array];
            for (NSInteger j = 0; j < firsArr.count; j++) {
                NSArray *secArray = [firsArr[j] componentsSeparatedByString:@":"];
                [ssA addObject:secArray.firstObject];
                [rrA addObject:secArray.lastObject];
                
            }
            model.lvMoneyArray = ssA;
            model.dateArray = rrA;
            model.tapSelected = NO;
            model.selectedIndex = 0;
        }
        
        [self cj_reloadData];
        
    }];
}

- (NSInteger)cj_numberOfSections{
    return 2;
}

- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section{
    if (section == 0) {
        return self.dataArray.count;
    }
    if (section == 1) {
        return 2;
    }
    return 0;
    
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}


- (CGFloat)cj_sectionFooterHeightAtSection:(NSInteger)section{
    if (section == 1) {
        return 100;
    }else{
        return 0;
    }
    
}

- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MyTapBiResuleTableViewCell *cell = [MyTapBiResuleTableViewCell cellWithTableView:self.tableView];
        MyTapBiClassModel *model = self.dataArray[indexPath.row];
        cell.lab1.text = model.GRADE;
        cell.lab2.text = model.lvMoneyArray[model.selectedIndex];
        NSLog(@"%ld",model.GRADESTATE);
        if (model.GRADESTATE == 1) {
            [cell.btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [cell.btn setTitle:@"暂停使用" forState:UIControlStateNormal];
        }else{
            [cell.btn setTitleColor:[UIColor colorWithHexString:kNomalTextBlackColor] forState:UIControlStateNormal];
            [cell.btn setTitle:model.dateArray[model.selectedIndex] forState:UIControlStateNormal];
            cell.TapBtnBlock = ^{
                [BRStringPickerView showStringPickerWithTitle:@"查看周期"
                                                   dataSource:model.dateArray
                                              defaultSelValue:@""
                                                  resultBlock:^(id selectValue) {
                                                      for (NSInteger i = 0; i<model.dateArray.count; i++) {
                                                          if ([selectValue isEqualToString:model.dateArray[i]]) {
                                                              model.selectedIndex = i;
                                                              [self.tableView cj_reloadSingleRowAtIndexPath:indexPath];
                                                          }
                                                      }
                                                  }];
            };
            
            
        };
        return cell;
    }
    if (indexPath.section == 1) {
        
        NSArray *titleArray = @[@"储存金额:",@"选择周期"];
        
        VESaveMoneyTableViewCell *cell = [VESaveMoneyTableViewCell cellWithTableView:self.tableView];
        
        cell.titleName.text = titleArray[indexPath.row];
        
        if (indexPath.row == 0) {
            cell.jiantou.hidden = YES;
            cell.enterText.placeholder = @"请输入金额";
            cell.enterText.keyboardType = UIKeyboardTypeNumberPad;
            cell.enterText.secureTextEntry = YES;
            [cell.enterText addTarget:self action:@selector(textChange:) forControlEvents:(UIControlEventEditingChanged)];
        } else {
            cell.jiantou.hidden = NO;
            cell.enterText.userInteractionEnabled = NO;
            self.guige = cell.enterText;
        }
        return cell;

    }
    return nil;

}

- (UIView *)cj_footerAtSection:(NSInteger)section{
    if (section == 1) {
        DTLongButtonView *view = [DTLongButtonView shareView];
        [view.tapBtn setTitle:@"存币" forState:UIControlStateNormal];
        view.tapBtnBlock = ^{
            NSLog(@"确 定");
            
            if (self.enterMoney < 100) {
                return [CJUIKit showErrorWithStatus:@"请输入最小存储金额"];
            }
            
            if (self.guigeStr.length == 0) {
                return [CJUIKit showErrorWithStatus:@"请选择规格"];
            }
            
            
            NSArray *arr = [self.guigeStr componentsSeparatedByString:@":"];
            NSCharacterSet* nonDigits =[[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            int remainSecond =[[[arr lastObject] stringByTrimmingCharactersInSet:nonDigits] intValue];
       
            NSDictionary *dic = @{
                                  @"Bishu": [NSString stringWithFormat:@"%0.0f",self.enterMoney],
                                  @"Zhouqi": [NSString stringWithFormat:@"%d",remainSecond],
                                  @"Rifuli": arr.firstObject,
                                  @"Userid": [CJUserInfoManager sharedManager].currentUserInfo.USERID
                                  };

            NSString *url = [NSString stringWithFormat:@"%@/VEService/SaveBishu.do",kPublicInterfaceAPI];
            
            

            CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
            [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
                [self cj_endRefresh];
                [self cj_endLoadMore];
            
                [CJUIKit showSuccessWithStatus:message];
                [self pop];
            }];
            
        };
        return view;
    }
    return nil;
}


- (void)textChange:(BRTextField *)text {
    
    
   text.secureTextEntry = NO;
    
    double inputMoney = [text.text doubleValue];
    
    
    double myMoney = [CJUserInfoManager sharedManager].currentUserInfo.ACCOUNTBALANCE;
    
    if (inputMoney > myMoney) {
        text.text = [NSString stringWithFormat:@"%0.0f",myMoney];
        inputMoney = myMoney;
    }
    
    self.enterMoney = inputMoney;
    
    if (self.guigeStr.length > 0) {
        self.guige.text = @"";
    }
//    [self.tableView reloadData];
    
}


- (void)cj_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(CJBaseTableViewCell *)cell {
    
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            if (self.enterMoney >= 100) {
                
                NSArray *guigeArr = [NSArray new];
                
                for (MyTapBiClassModel *model in self.dataArray) {
                    
                    NSArray *arr = [model.GRADE componentsSeparatedByString:@"-"];
                    
                    if (self.enterMoney >= [[arr firstObject] doubleValue] && self.enterMoney <= [[arr lastObject] doubleValue]) {
                        guigeArr = model.hunheArray;
                    }
                    
                }
                
                if (guigeArr.count == 0) {
                    return [CJUIKit showErrorWithStatus:@"请输入范围之类的数量"];
                }
                
                WEAKSELF
                [BRStringPickerView showStringPickerWithTitle:@"规格"
                                                   dataSource:guigeArr
                                              defaultSelValue:@""
                                                  resultBlock:^(id selectValue) {
                                                      weakSelf.guige.text = selectValue;
                                                      weakSelf.guigeStr = selectValue;
                                                  }];
                
                
                
            } else {
                return [CJUIKit showErrorWithStatus:@"请输入最小存储金额"];
            }
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
